#include <stdio.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "hi_io.h"
#include "hi_gpio.h"
#include "buzzer.h"
#include "hal_bsp_ssd1306.h"
#include "led.h"
#include "fan.h"
#include "ps.h"
int flag = 0;   // 开关标记
int status = 0; // 开关状态
void myfunc(void *arg)
{
    printf("PRESS KEY.\n");
    if (flag == 0)
    {
        // buzzer_on();
        flag = 1;
    }
    else
    {
        // buzzer_off();
        flag = 0;
    }
}
void mysetgpio(void *arg)
{
    hi_u32 ret;
    // 初始化,设置基本IO功能
    int value;
    ret = hi_io_set_func(HI_IO_NAME_GPIO_14, HI_IO_FUNC_GPIO_14_GPIO);
    if (ret != 0)
    {
        printf("set gpio14 failed\n");
        return;
    }
    // 设置默认上拉，高电平
    ret = hi_io_set_pull(HI_IO_NAME_GPIO_14, HI_IO_PULL_UP);
    if (ret != 0)
    {
        printf("set gpio14 pull failed\n");
        return;
    }
    // 消除抖动
    ret = hi_io_set_schmitt(HI_IO_NAME_GPIO_14, HI_TRUE);
    if (ret != 0)
    {
        printf("set gpio14 filter failed\n");
        return;
    }
    // gpio初始化
    ret = hi_gpio_init();
    if (ret != 0)
    {
        printf("gpio init failed\n");
        return;
    }
    // 设置输入方向
    ret = hi_gpio_set_dir(HI_GPIO_IDX_14, HI_GPIO_DIR_IN);
    if (ret != 0)
    {
        printf("set gpio14 dir failed\n");
        return;
    }
    // 设置中断捕获模式
    ret = hi_gpio_register_isr_function(HI_GPIO_IDX_14, HI_INT_TYPE_EDGE, HI_GPIO_EDGE_FALL_LEVEL_LOW, myfunc, NULL);
    if (ret != 0)
    {
        printf("set func failed\n");
        return;
    }

    while (1)
    {

        if (flag == 1)
        {
            if (status == 0)
            {
                buzzer_on(4); //buzzer_on 需要传入 4 来开启蜂鸣器
                fan_on(7);    //fan_on 需要传入 7 来开启风扇
                led_on(1);    //led_on 需要传入 1 来开启 LED
                status = 1;
                SSD1306_ShowStr(0, 0, "BUZZER ON ", TEXT_SIZE_16);
                SSD1306_ShowStr(0, 16, "FAN ON     ", TEXT_SIZE_16);
                SSD1306_ShowStr(0, 32, "LED ON     ", TEXT_SIZE_16);
           }
        }
        else
        {
            if (status == 1)
            {
                buzzer_off(); // 关闭蜂鸣器
                fan_off();    // 关闭风扇
                led_off();    // 关闭 LED
                status = 0;
                SSD1306_ShowStr(0, 0, "BUZZER OFF", TEXT_SIZE_16);
                SSD1306_ShowStr(0, 16, "FAN OFF    ", TEXT_SIZE_16);
                SSD1306_ShowStr(0, 32, "LED OFF    ", TEXT_SIZE_16);
            
            }
        }
        //usleep(100000);
        value = ps_getdata();
        printf("value:%d\n", value);
        sleep(1);
    }
}

// main函数
void mygpio(void)
{
    osThreadAttr_t attr;
    osThreadId_t id;
    int ret;
    // 初始化buzzer
    buzzer_init();
    // 初始化led
    led_init();
    // 初始化fan
    fan_init();
     
    //显示屏初始化
    ret=SSD1306_Init();
    if(ret!=0)
    {
        printf("SSD1306_Init failed\n");
        return;
    }
    SSD1306_CLS();

//人体接近初始化
ps_init();

    attr.name = "mygpio";
    attr.cb_mem = NULL;
    attr.cb_size = 0;
    attr.stack_mem = NULL;
    attr.stack_size = 1024*5;
    attr.attr_bits = 0;
    attr.priority = osPriorityNormal;
    // 创建线程

    id = osThreadNew(mysetgpio, NULL, &attr);
    if (id == NULL)
    {
        printf("Create Thread failed.\n");
        return;
    }
    printf("Create Thread failed.\n");
}
SYS_RUN(mygpio);
